import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

@Setter
@Getter
public class ChatClient {
    Socket socket;
    ObjectOutputStream oos;
    ObjectInputStream ois;
    boolean connected_status=false;
    public static void main(String[] args) throws InterruptedException {

            ChatClient client = new ChatClient();
            while(!client.isConnected_status()){
                System.out.println("正在连接服务器...");
            client.connect();
            Thread.sleep(5000);
            }

            //引入发送和接收线程
            new Thread(new ClientSend(client.getOos())).start();
            new Thread((new ClientRecv(client.getOis()))).start();


    }

    private void connect()  {
        try {
        socket = new Socket("localhost", 9900);
        connected_status=true;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的名称:");
        String name = sc.nextLine();
        oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(name);
        oos.flush();

        //接收客户端发来的信息
        ois = new ObjectInputStream(socket.getInputStream());
        System.out.println(ois.readObject());
        } catch (IOException e) {

        } catch (ClassNotFoundException e) {

        }
    }
}
